<?php
class youhui_signup extends spController
{
	function __construct()
	{
	        // 必须加入启动父类构造函数的操作
	        parent::__construct();
	         
	        // 开始全局操作
	        $this->arr_cat = Store_Cat();
			$this->a = $_GET['a'];
			$this->c = $_GET['c'];
	}

	function main()
	{
		$m_youhui_signup = spClass("m_youhui_signup");
		$this->results = $m_youhui_signup->spPager($this->spArgs('page', 1), 60)->findAll('', 'ysid DESC');
		$this->pager = $m_youhui_signup->spPager()->getPager();
		$this->display("admin/youhuisignu_list.html");
	}

	function add()
	{		
		if(!empty($_POST)) {
			$m_attachment = spClass('attachment');
			$_POST['thumb'] = $m_attachment->upload('thumb');
			
			$m_youhui_signup = spClass("m_youhui_signup");
		//	$_POST['title'] = $m_youhui_signup->escape($_POST['title']);
		//	$_POST['store_name'] = $m_youhui_signup->escape($_POST['store_name']);
		//	$_POST['link'] = $m_youhui_signup->escape($_POST['link']);
			$m_youhui_signup->create($_POST);
			$this->success('感谢您的报名', spUrl('youhui_signup', 'add'));
		} else {
			$this->display("add_youhuisignup.html");
		}
	}

	function view()
	{
		$this->ysid = $ysid = intval($_GET['ysid']);
		$m_youhui_signup = spClass("m_youhui_signup");
		$this->result = $m_youhui_signup->find(array('ysid'=>$ysid));
		$this->display("admin/view_youhuisignup.html");
	}

	/*
	 *	审核通过	
	 */
	function verify()
	{
		if(!$_POST) {
			$this->ysid = $ysid = intval($_GET['ysid']);
			if($ysid < 1) return false;
			$m_youhui_signup = spClass("m_youhui_signup");
			$this->result = $m_youhui_signup->find(array('ysid'=>$ysid));
		}

		if($_GET['step'] == 1) {
			if(!empty($_GET['store_id'])) {
				$store_id = $_GET['store_id'];
				if(!$store_id) $this->error('店铺不存在', spUrl('youhuisignup', 'verify'));
				$url = spUrl('youhui_signup', 'verify', array('store_id'=>$store_id, 'step'=>2));
				$this->jump(spUrl('youhui_signup', 'verify', array('store_id'=>$store_id, 'step'=>2, 'ysid'=>$ysid)));
			} else {
				$this->display("admin/youhuisignup_post_step1.html");
			}
		} elseif($_GET['step'] == 2 || $_POST['step'] == 2) {
			if(!empty($_POST)) {
				$m_youhui = spClass("youhui");
				$m_attachment = spClass('attachment');

				$verifier = $m_youhui->spVerifier($_POST);
				if( false != $verifier) {
					$this->error('提交数据不正确', spUrl('admin', 'post'));
				} else {
					if($_FILES['thumb']['size'] > 0) {
						$_POST['thumb'] = $m_attachment->upload('thumb');
					} else {
						$_POST['thumb'] = $_POST['thumb_verify'];
					}
					if(empty($_POST['thumb'])) {
						$this->error('提交数据不正确', spUrl('admin', 'post'));
					}
					if($_POST['catid'] <= 0) {
						$this->error('提交数据不正确', spUrl('admin', 'post'));
					}
					$_POST['createtime'] = date('Y-m-d H:i:s', time());
				
					if($_POST['url'] && strpos($_POST['url'], '?')) {
							$url = substr($_POST['url'], strpos($_POST['url'], '?')+1);
							parse_str($url, $arr_url);
							if(!in_array($arr_url['p'], array('mm_30161668_0_0', 'mm_29938918_0_0')) && !empty($arr_url['p'])) $this->error('提交的pid不正确', spUrl('admin', 'edit', array('pid'=>$_POST['pid'])));
					}

					$pid = $m_youhui->create($_POST);
					if($_POST['posid']) {
						$arr_pos[$pid] = $_POST['posid'];
						$this->position($arr_pos);
					}
					$this->success('提交成功', spUrl('admin', 'listinfo'));
				}
			} else {
				if($_GET['store_id']) {
					$store_id = $_GET['store_id'];
					$m_store = spClass("m_store");
					$this->store_result = $m_store->find(array('store_id'=>$store_id));
				}
				$this->display("admin/youhuisignup_post_step2.html");
			}
		}
	}

	/*
	 *	店铺是否存在
	 */
	function store_check()
	{
		$m_store = spClass("m_store");
		$store_name = $_GET['store_name'];
		$result = $m_store->find(array('store_name'=>$store_name));
		if(!$result) exit('-1');
		exit($result['store_id']);
	}
}
?>